// pages/changepwd/changepwd.js
const db=wx.cloud.database().collection('login')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tip:'',
    tip_content:'',
    head_src:'',
    old:'',
    new:'',
    confirm:''
  },
  change:function(){
    const that=this
    var old=that.data.old
    var _new=that.data.new
    var confirm=that.data.confirm
    if(!old){
      that.tip('请输入原密码！')
      return
    }
    if(!_new){
      that.tip('请输入新密码！')
      return
    }
    if(!confirm){
      that.tip('请输入确认密码！')
      return
    }
    if(_new==confirm){
      const user=wx.getStorageSync('user')
      db.where({
        stuno:user
      }).get({
        success:function(res){
          if(old==res.data[0].pwd){
            db.doc(res.data[0]._id).update({
              data:{
                pwd:confirm
              },
              success:function(res){
                console.log(res)
                that.tip('密码修改成功！')
                that.setData({
                  old:'',
                  new:'',
                  confirm:''
                })
                wx.navigateBack({
                  delta: 0,
                })
              }
            })
          }else{
            that.tip('原密码错误！')
            that.setData({
              old:''
            })
            return
          }
        }
      })
    }else {
      that.tip('两次密码不一致！')
      that.setData({
        new:'',
        confirm:''
      })
      return
    }
  },
  old_in:function(e){
    this.data.old=e.detail.value
  },
  new_in:function(e){
    this.data.new=e.detail.value
  },
  confirm_in:function(e){
    this.data.confirm=e.detail.value
  },
  tip:function (str){
    const that=this
    that.setData({
      tip_content:str,
      tip:true
    })
    setTimeout(function(){
      that.setData({
        tip:false
      })
    },1000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      head_src:options.head_src
    })
  },


})